package no.ntnu.fp.gui;

import java.awt.Canvas;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.miginfocom.swing.MigLayout;
import no.ntnu.fp.gui.guiElements.CalendarPanel;
import no.ntnu.fp.gui.guiElements.CalendarPanel2;
import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.Message;
import no.ntnu.fp.model.Person;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.LinkedList;
import java.util.List;

public class CommandCentre implements ClockHolder {

	private JFrame frame;
	private JTextField txtSearchField;
	private String currTime = "00:00";
	private static CalendarPanel2 calendarPanel;
	private JLabel lblMonth;
	private JLabel clockLabel;
	private Clock aClock;
	private static JLabel weekMonthlbl;
	private JLabel lblDate;
	public static final String aeoeaaAEOEAA = "\u00E6\u00F8\u00E5\u00C6\u00D8\u00C5";
	private EmployeeCalendarSelectPanel employeesPanel;

	/**
	 * Launch the application.
	 */
	public static void start() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CommandCentre window = new CommandCentre();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CommandCentre() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		aClock = new Clock(this);
		frame = new JFrame();
		frame.setBounds(100, 100, 858, 478);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(
				new MigLayout("",
						"[][100px,grow 15][grow][150px:250px,grow 15]",
						"[][60px][][][grow][][][][grow][]"));
		JButton btnAddAppointment = new JButton("Ny avtale");
		btnAddAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				prosessAction("addAppointment");
			}
		});
		
		employeesPanel = new EmployeeCalendarSelectPanel();
		calendarPanel = new CalendarPanel2(frame, employeesPanel.getList());
		frame.getContentPane().add(btnAddAppointment, "flowy,cell 1 1,grow");

		employeesPanel.getList().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent ev) {
//				Person selectedPerson = (Person) employeesPanel.getList().getSelectedValue();
				calendarPanel.draw();
			}
		});
		frame.getContentPane().add(employeesPanel, "cell 3 3 1 6,grow");
		employeesPanel.setLayout(new MigLayout("", "[grow,center]",
				"[22px][60px]"));
		
		

		JPanel eventPanel = new EventPanel();
		frame.getContentPane().add(eventPanel, "cell 2 1,grow");

		JButton btnLogout = new JButton("Logg ut");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				prosessAction("logout");
			}
		});
		frame.getContentPane().add(btnLogout, "flowy,cell 3 1,grow");

		lblDate = new JLabel("           " + calendarPanel.getFullDate());
		frame.getContentPane().add(lblDate, "cell 1 2");
		lblDate.setFont(new Font("SansSerif", Font.PLAIN, 12));

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, "cell 2 2,grow");
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JButton button_1 = new JButton("<-");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendarPanel.updatePanelBackwardDay();
				updateWeekMonth();
			}
		});

		JButton button = new JButton("<<-");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendarPanel.updatePaneBackwardWeek();
				updateWeekMonth();

			}
		});

		JButton button_4 = new JButton("I dag");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				calendarPanel.refreshCalendar();
				updateWeekMonth();
			}
		});
		GridBagConstraints gbc_button_4 = new GridBagConstraints();
		gbc_button_4.insets = new Insets(0, 0, 0, 5);
		gbc_button_4.gridx = 0;
		gbc_button_4.gridy = 0;
		panel.add(button_4, gbc_button_4);
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(0, 0, 0, 5);
		gbc_button.gridx = 2;
		gbc_button.gridy = 0;
		panel.add(button, gbc_button);
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.insets = new Insets(0, 0, 0, 5);
		gbc_button_1.gridx = 3;
		gbc_button_1.gridy = 0;
		panel.add(button_1, gbc_button_1);

		JButton button_3 = new JButton("->");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendarPanel.updatePanelForwardDay();
				updateWeekMonth();
			}
		});
		GridBagConstraints gbc_button_3 = new GridBagConstraints();
		gbc_button_3.insets = new Insets(0, 0, 0, 5);
		gbc_button_3.gridx = 5;
		gbc_button_3.gridy = 0;
		panel.add(button_3, gbc_button_3);

		JButton button_2 = new JButton("->>");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendarPanel.updatePanelForwardWeek();
				updateWeekMonth();
			}
		});
		GridBagConstraints gbc_button_2 = new GridBagConstraints();
		gbc_button_2.insets = new Insets(0, 0, 0, 5);
		gbc_button_2.gridx = 6;
		gbc_button_2.gridy = 0;
		panel.add(button_2, gbc_button_2);
		weekMonthlbl = new JLabel("Uke: " + calendarPanel.getWeek()
				+ ", M\u00E5ned: " + calendarPanel.getMonth(0) + ", \u00C5r: "
				+ calendarPanel.getYear());
		GridBagConstraints gbc_weekMonthlbl = new GridBagConstraints();
		gbc_weekMonthlbl.insets = new Insets(0, 0, 0, 5);
		gbc_weekMonthlbl.gridx = 16;
		gbc_weekMonthlbl.gridy = 0;
		panel.add(weekMonthlbl, gbc_weekMonthlbl);

		final MessageContainer messageContainer = new MessageContainer();

		for (Message m : Message.getAllTo(Person.getCurrentUser())) {
			if (m.active)
				messageContainer.add(new MessageLabel(m, messageContainer));
		}

		JScrollPane jsp = new JScrollPane(messageContainer);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		frame.getContentPane().add(jsp, "cell 1 3 1 6,grow");

		frame.getContentPane().add(calendarPanel, "cell 2 3 1 7,grow");

		JLabel lblName = new JLabel(Person.getCurrentUser().getName());
		lblName.setForeground(Color.GRAY);
		lblName.setFont(new Font("Dialog", Font.BOLD, 13));
		frame.getContentPane().add(lblName, "cell 3 1,alignx right");

		clockLabel = new JLabel(currTime);
		clockLabel.setFont(new Font("SansSerif", Font.BOLD, 17));
		clockLabel.setHorizontalAlignment(SwingConstants.CENTER);
		frame.getContentPane().add(clockLabel, "cell 1 1,alignx center");

	}

	public static void updateWeekMonth() {

		weekMonthlbl.setText("Uke: " + calendarPanel.getWeek()
				+ ", M\u00E5ned: " + calendarPanel.getMonth(0) + ", \u00C5r: "
				+ calendarPanel.getYear());
	}

	public void prosessAction(String buttonName) {

		if (buttonName.equals("logout")) {
			frame.dispatchEvent(new WindowEvent(frame,
					WindowEvent.WINDOW_CLOSING));
		} else if (buttonName.equals("addAppointment")) {
			
			NewAppointmentWindow naw = new NewAppointmentWindow(Appointment.blank());
		}
	}

	@Override
	public void setTime(String theTime) {
		currTime = theTime;
		if (clockLabel != null) {
			clockLabel.setText(currTime);
			clockLabel.repaint();
		}
	}
}
